package com.example.othree.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.*;
import com.example.othree.entity.CropsYieldYearData;
import com.example.othree.service.CropsYieldYearDataService;
import com.example.othree.service.MainCropsHsService;
import com.example.othree.service.MainCropsXtService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import javax.management.QueryEval;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zogran
 * @since 2024-11-12
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class CropsYieldYearDataController {
    @Autowired
    private CropsYieldYearDataService cropsYieldYearDataService;

    @Autowired
    private MainCropsHsService mainCropsHsService;

    @Autowired
    private MainCropsXtService mainCropsXtService;

    @PostMapping("/crops/create")
    @ApiOperation("新增")
    public R create(@RequestBody CropsYieldYearData cropsYieldYearData){
        return cropsYieldYearDataService.save(cropsYieldYearData) ? R.ok() : R.error();
    }

    @PostMapping("/crops/saveBatch")
    @ApiOperation("批量新增")
    public R saveBatch(@RequestBody List<CropsYieldYearData> cropsYieldYearDataList){
        return cropsYieldYearDataService.saveBatch(cropsYieldYearDataList) ? R.ok() : R.error();
    }

    @GetMapping("/crops/list")
    public List<CropsYieldYearData> list(){
        return cropsYieldYearDataService.list();
    }

    @PostMapping("/crops/update")
    @ApiOperation("修改")
    public R update(@RequestBody CropsYieldYearData cropsYieldYearData){
        return cropsYieldYearDataService.updateById(cropsYieldYearData) ? R.ok() : R.error();
    }

    @PostMapping("/crops/delete/{id}")
    public R delete(@PathVariable Integer id){
        return cropsYieldYearDataService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/crops/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String year = param.get("year").toString();
        String city = param.get("city").toString();

        Page<CropsYieldYearData> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<CropsYieldYearData> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(year) && !"null".equals(year)){
            lambdaQueryWrapper.eq(CropsYieldYearData::getYear, year);
        }
        if(StringUtils.isNotBlank(city) && !"null".equals(city)){
            lambdaQueryWrapper.like((CropsYieldYearData::getCity), city);
        }

        IPage result = cropsYieldYearDataService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }

    @GetMapping("/crops-year-data-page")
    @ApiOperation("获取邢台或衡水的农作物逐年种产数据")
    public R cropsYearDataPage(@RequestParam("year") String year,
                               @RequestParam("city") String city,
                               @RequestParam("crop") String crop,
                               @RequestParam("page") int current,
                               @RequestParam("pageSize") int pageSize) {

        if (city.equals("衡水")) {

            QueryWrapper<MainCropsHs> mainCropsHsQueryWrapper = new QueryWrapper<>();
            mainCropsHsQueryWrapper.eq("year", year);
            mainCropsHsQueryWrapper.eq("crops_name", crop);

            Page<MainCropsHs> page = new Page<>();
            page.setCurrent(current);
            page.setSize(pageSize);

            Page<MainCropsHs> res = mainCropsHsService.pageList(page, mainCropsHsQueryWrapper);
            List<MainCropsHs> records = res.getRecords();

            return R.ok().data("data", records).data("currentPage", current).data("total", res.getTotal()).
                    data("pageNum", page.getPages());



        } else if (city.equals("邢台")) {
            QueryWrapper<MainCropsXt> mainCropsHsQueryWrapper = new QueryWrapper<>();
            mainCropsHsQueryWrapper.eq("year", year);
            mainCropsHsQueryWrapper.eq("crops_name", crop);

            Page<MainCropsXt> page = new Page<>();
            page.setCurrent(current);
            page.setSize(pageSize);

            Page<MainCropsXt> res = mainCropsXtService.pageList(page, mainCropsHsQueryWrapper);
            List<MainCropsXt> records = res.getRecords();

            return R.ok().data("data", records).data("currentPage", current).data("total", res.getTotal()).
                    data("pageNum", page.getPages());

        }
        return R.ok();
    }


}

